home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / xhist / comp_hist.c next >
Encoding:
C/C++ Source or Header  |  1991-09-17  |  20.9 KB  |  940 lines

  1.  
  2. #include "comp_hist.h"
  3.  
  4. comp_hist()
  5. {                /* to compute histogram      */
  6.     int       i;
  7.  
  8.     int       pv;        /* pixel value     */
  9.  
  10.     if (pix_format == PFBYTE) {
  11.     for (i = 0; i < 256; i++)
  12.         b_hist[i] = 0;    /* initial     */
  13.     val_min = barray[0];
  14.     val_Max = barray[0];
  15.  
  16.     n_of_dv = 0;        /* number of different values     */
  17.     for (i = 0; i < size; i++) {
  18.         pv = barray[i];    /* pixel value         */
  19.  
  20.         if (b_hist[pv] == 0)
  21.         n_of_dv++;
  22.  
  23.         b_hist[pv]++;
  24.  
  25.         if (pv < val_min)
  26.         val_min = pv;
  27.         if (val_Max < pv)
  28.         val_Max = pv;
  29.     }
  30.  
  31.     free(barray);
  32.  
  33.     occ_Max = 0;        /* occurrence Maximum         */
  34.     for (i = val_min; i <= val_Max; i++)
  35.         if (occ_Max < b_hist[i]) {
  36.         occ_Max = b_hist[i];
  37.         occ_Max_i = i;
  38.         }
  39.     set_str();        /* set hint strings     */
  40.     return;
  41.     }                /* end of if byte         */
  42.     if (pix_format == PFSHORT) {
  43.     s_hist = Calloc(65536, int);
  44.     for (i = 0; i < 65536; i++)
  45.         s_hist[i] = 0;    /* initial     */
  46.     val_min = sarray[0];
  47.     val_Max = sarray[0];
  48.  
  49.     n_of_dv = 0;        /* number of different values     */
  50.     for (i = 0; i < size; i++) {
  51.         pv = sarray[i] + 32768;    /* pixel value         */
  52.  
  53.         if (s_hist[pv] == 0)
  54.         n_of_dv++;
  55.  
  56.         s_hist[pv]++;
  57.  
  58.         if (sarray[i] < val_min)
  59.         val_min = sarray[i];
  60.         if (val_Max < sarray[i])
  61.         val_Max = sarray[i];
  62.     }
  63.  
  64.     free(sarray);
  65.  
  66.     occ_Max = 0;        /* occurrence Maximum         */
  67.     for (i = val_min + 32768; i <= val_Max + 32768; i++)
  68.         if (occ_Max < s_hist[i]) {
  69.         occ_Max = s_hist[i];
  70.         occ_Max_i = i - 32768;
  71.         }
  72.     set_str();        /* set hint strings     */
  73.     return;
  74.     }                /* end of if short         */
  75.     if (pix_format == PFINT) {
  76.     int_hist();
  77.  
  78. #ifdef aaz
  79.     printf("end of comp_hist, after int_hist, val_min=%d  val_Max=%d\n",
  80.            (int) val_min, (int) val_Max);
  81.     printf("             n_of_dv=%d  occ_Max=%d  occ_Max_i=%d\n\n",
  82.            n_of_dv, occ_Max, occ_Max_i);
  83. #endif
  84.  
  85.     return;
  86.     }
  87.     /* float     */
  88.     float_hist();        /* to compute float histogram f_hist[]         */
  89.  
  90. #ifdef aaz
  91.     printf("end of comp_hist, after float_hist, val_min=%.2f  val_Max=%.2f\n",
  92.        val_min, val_Max);
  93.     printf("             n_of_dv=%d  occ_Max=%d  occ_Max_f=%.2f\n\n",
  94.        n_of_dv, occ_Max, occ_Max_f);
  95. #endif
  96.  
  97. }                /* end of  comp_hist ()     */
  98.  
  99. int_hist()
  100. {
  101.     int       i;
  102.     int       pv;        /* pixel value     */
  103.  
  104.     Ihtype   *q, *r;        /* int histogram     */
  105.  
  106.     val_min = iarray[0];
  107.     val_Max = iarray[0];
  108.  
  109.     for (i = 0; i < size; i++) {
  110.     pv = iarray[i];        /* int pixel value       */
  111.     if (val_min > pv)
  112.         val_min = pv;
  113.     if (val_Max < pv)
  114.         val_Max = pv;
  115.     }
  116.  
  117.     cnt_size = val_Max - val_min + 1;    /* pv_cnt[] size   */
  118.     pv_cnt = Calloc(cnt_size, int);
  119.     if (pv_cnt != NULL) {
  120.     i_bucket();
  121.     return;
  122.     }
  123.     i0_hist = NULL;
  124.     for (i = 0; i < size; i++)
  125.     ins_i(iarray[i]);    /* insert int  */
  126.     free(iarray);
  127.  
  128.     q = i0_hist;
  129.     i_hist = Calloc(n_of_dv, Ihtype_a);
  130.  
  131.     occ_Max = 0;        /* occurrence Maximum         */
  132.     for (i = 0; i < n_of_dv; i++) {
  133.     i_hist[i].pv = q->pv;
  134.     i_hist[i].occ = q->occ;
  135.     if (occ_Max < i_hist[i].occ) {
  136.         occ_Max = i_hist[i].occ;
  137.         occ_Max_i = i_hist[i].pv;
  138.     }
  139.     r = q->next;
  140.     free(q);
  141.     q = r;
  142.     }
  143.  
  144.     set_str();            /* set hint strings     */
  145. }                /* end of int_hist ()         */
  146.  
  147. i_bucket()
  148. {                /* int bucket sort     */
  149.     int       i;
  150.     int       k;
  151.     int       y;
  152.     int       pv_min;
  153.  
  154.     pv_min = val_min;
  155.     for (i = 0; i < cnt_size; i++)
  156.     pv_cnt[i] = 0;
  157.     n_of_dv = 0;        /* number of different values     */
  158.  
  159.     for (i = 0; i < size; i++) {
  160.     k = iarray[i] - pv_min;    /* k corresponds to pixel value.  */
  161.     if (pv_cnt[k] == 0)
  162.         n_of_dv++;
  163.     pv_cnt[k]++;
  164.     }
  165.     free(iarray);
  166.  
  167.     i_hist = Calloc(n_of_dv, Ihtype_a);
  168.     y = 0;
  169.     for (k = 0; k < cnt_size; k++) {    /* k corresponds to pixel value.  */
  170.     if (pv_cnt[k] > 0) {
  171.         i_hist[y].pv = pv_min + k;    /* int pixel value     */
  172.         i_hist[y].occ = pv_cnt[k];    /* count     */
  173.         y++;
  174.     }
  175.     }
  176.     /* printf("in i_bucket, n_of_dv=%d  y=%d\n", n_of_dv, y ); */
  177.     free(pv_cnt);
  178.  
  179.     occ_Max = 0;        /* occurrence Maximum           */
  180.     for (i = 0; i < n_of_dv; i++)
  181.     if (occ_Max < i_hist[i].occ) {
  182.         occ_Max = i_hist[i].occ;
  183.         occ_Max_i = i_hist[i].pv;
  184.     }
  185.     set_str();            /* set hint strings     */
  186. }                /* end of   i_bucket ()         */
  187.  
  188. float_hist()
  189. {                /* to compute float histogram          */
  190.     int       i;
  191.     float     pv;        /* pixel value     */
  192.  
  193.     Fhtype   *s, *t;        /* float histogram     */
  194.  
  195.     val_min = floor(farray[0] * 100.);
  196.     val_Max = floor(farray[0] * 100.);
  197.  
  198.     for (i = 0; i < size; i++) {
  199.     pv = farray[i];        /* float pixel value     */
  200.     pv = floor(pv * 100.);
  201.     farray[i] = pv;
  202.  
  203.     if (val_min > pv)
  204.         val_min = pv;
  205.     if (val_Max < pv)
  206.         val_Max = pv;
  207.     }
  208.  
  209.     cnt_size = val_Max - val_min + 1;    /* pv_cnt[] size     */
  210.     pv_cnt = Calloc(cnt_size, int);
  211.     if (pv_cnt != NULL) {
  212.     f_bucket();
  213.     return;
  214.     }
  215.     val_min = val_min / 100.;
  216.     val_Max = val_Max / 100.;
  217.  
  218.     f0_hist = NULL;
  219.     for (i = 0; i < size; i++)
  220.     ins_f(i);        /* insert int  */
  221.     free(farray);
  222.  
  223.     s = f0_hist;
  224.     f_hist = Calloc(n_of_dv, Fhtype_a);
  225.  
  226.     occ_Max = 0;        /* occurrence Maximum         */
  227.     for (i = 0; i < n_of_dv; i++) {
  228.     f_hist[i].pv = s->pv / 100.;
  229.     f_hist[i].occ = s->occ;
  230.     if (occ_Max < f_hist[i].occ) {
  231.         occ_Max = f_hist[i].occ;
  232.         occ_Max_f = f_hist[i].pv;
  233.     }
  234.     t = s->next;
  235.     free(s);
  236.     s = t;
  237.     }
  238.  
  239.     set_str();            /* set hint strings     */
  240. }                /* end of float_hist ()     */
  241.  
  242. f_bucket()
  243. {                /* float bucket sort     */
  244.     int       i;
  245.     int       k;
  246.     int       y;
  247.  
  248.     for (i = 0; i < cnt_size; i++)
  249.     pv_cnt[i] = 0;
  250.  
  251.     n_of_dv = 0;        /* number of different values     */
  252.  
  253.     for (i = 0; i < size; i++) {
  254.     k = farray[i] - val_min;
  255.     if (pv_cnt[k] == 0)
  256.         n_of_dv++;
  257.     pv_cnt[k]++;
  258.     }
  259.     free(farray);
  260.  
  261.     f_hist = Calloc(n_of_dv, Fhtype_a);
  262.     y = 0;
  263.     for (i = 0; i < cnt_size; i++) {
  264.     if (pv_cnt[i] > 0) {
  265.         f_hist[y].pv = (val_min + i) / 100.;    /* float pixel value  */
  266.         f_hist[y].occ = pv_cnt[i];    /* count     */
  267.         y++;
  268.     }
  269.     }
  270.     /* printf("in f_bucket, n_of_dv=%d  y=%d\n", n_of_dv, y );  */
  271.     free(pv_cnt);
  272.     val_min = val_min / 100.;
  273.     val_Max = val_Max / 100.;
  274.  
  275.     occ_Max = 0;        /* occurrence Maximum           */
  276.     for (i = 0; i < n_of_dv; i++)
  277.     if (occ_Max < f_hist[i].occ) {
  278.         occ_Max = f_hist[i].occ;
  279.         occ_Max_f = f_hist[i].pv;
  280.     }
  281.     set_str();            /* set hint strings     */
  282. }                /* end of  f_bucket ()         */
  283.  
  284. ins_i(pv)            /* insert int pixel value     */
  285.     int       pv;
  286. {
  287.     Ihtype   *prev, *q, *r;    /* int histogram     */
  288.  
  289.     if (i0_hist == NULL) {
  290.     i0_hist = Calloc(1, Ihtype);
  291.     n_of_dv = 1;        /* number of different values         */
  292.  
  293.     i0_hist->pv = pv;    /* int pixel value     */
  294.     i0_hist->occ = 1;    /* occurrence         */
  295.     i0_hist->next = NULL;
  296.     return;
  297.     }
  298.     q = i0_hist;
  299.     while (q != NULL) {
  300.     if (pv < q->pv) {
  301.         r = Calloc(1, Ihtype);
  302.         n_of_dv++;
  303.         r->pv = pv;        /* int pixel value       */
  304.         r->occ = 1;        /* occurrence            */
  305.         r->next = q;
  306.         if (q == i0_hist)
  307.         i0_hist = r;
  308.         else
  309.         prev->next = r;
  310.         return;
  311.     }
  312.     if (pv == q->pv) {
  313.         q->occ++;
  314.         return;
  315.     }
  316.     prev = q;
  317.  
  318.     q = q->next;
  319.     }                /* end of while     */
  320.  
  321.     r = Calloc(1, Ihtype);
  322.     n_of_dv++;
  323.     r->pv = pv;            /* int pixel value       */
  324.     r->occ = 1;            /* occurrence            */
  325.     r->next = NULL;
  326.     prev->next = r;
  327. }                /* end of ins_i (pv)         */
  328.  
  329. ins_f(idx)            /* insert int pixel value     */
  330.     int       idx;        /* index of farray[]         */
  331. {
  332.     float     pv;
  333.     Fhtype   *prev, *q, *r;    /* int histogram     */
  334.  
  335.     pv = farray[idx];        /* float pixel value     */
  336.     /* pv = floor( pv * 100. ) / 100. ;  */
  337.  
  338.     if (f0_hist == NULL) {
  339.     f0_hist = Calloc(1, Fhtype);
  340.     n_of_dv = 1;        /* number of different values         */
  341.  
  342.     f0_hist->pv = pv;    /* float pixel value     */
  343.     f0_hist->occ = 1;    /* occurrence         */
  344.     f0_hist->next = NULL;
  345.     goto end_;
  346.     }
  347.     q = f0_hist;
  348.     while (q != NULL) {
  349.     if (pv < q->pv) {
  350.         r = Calloc(1, Fhtype);
  351.         n_of_dv++;
  352.         r->pv = pv;        /* float pixel value       */
  353.         r->occ = 1;        /* occurrence            */
  354.         r->next = q;
  355.         if (q == f0_hist)
  356.         f0_hist = r;
  357.         else
  358.         prev->next = r;
  359.         goto end_;
  360.     }
  361.     if (pv == q->pv) {
  362.         q->occ++;
  363.         goto end_;
  364.     }
  365.     prev = q;
  366.  
  367.     q = q->next;
  368.     }                /* end of while     */
  369.  
  370.     r = Calloc(1, Fhtype);
  371.     n_of_dv++;
  372.     r->pv = pv;            /* float pixel value       */
  373.     r->occ = 1;            /* occurrence            */
  374.     r->next = NULL;
  375.     prev->next = r;
  376. end_:
  377. #ifdef aay
  378.     if (idx % 1000 == 0)
  379.     printf("end of ins_f, i=%d  pv=%.2f  n_of_dv=%d\n",
  380.            idx, pv / 100., n_of_dv);
  381. #endif
  382.  
  383.     return;
  384. }                /* end of ins_f (idx)         */
  385.  
  386. set_str()
  387. {                /* set hint string  hint_s         */
  388.     int       i;
  389.  
  390.     paint = 0;            /* fisrt picture of this image  */
  391.  
  392.     sprintf(hint_s[0], "image file : %s", cur_fname);
  393.  
  394.     switch (pix_format) {
  395.     case PFBYTE:
  396.     sprintf(hint_s[1], "image type : byte");
  397.     break;
  398.     case PFSHORT:
  399.     sprintf(hint_s[1], "image type : short");
  400.     break;
  401.     case PFINT:
  402.     sprintf(hint_s[1], "image type : int");
  403.     break;
  404.     case PFFLOAT:
  405.     sprintf(hint_s[1], "image type : float");
  406.     break;
  407.     }
  408.  
  409.     sprintf(hint_s[2], "image size : %d x %d ,", nrow, ncol);
  410.     sprintf(hint_s[3], "             %d frame(s)", nfr);
  411.  
  412.     sprintf(hint_s[4], "total pixels : %d", size);
  413.     sprintf(hint_s[5], "# of different pixel values :");
  414.     sprintf(hint_s[6], "   %d", n_of_dv);
  415.     hint_s[7][0] = 0;
  416.  
  417.     sprintf(hint_s[8], "Maximum count = %d", occ_Max);
  418.  
  419.     if (pix_format == PFFLOAT)
  420.     sprintf(hint_s[9], "   at pixel value %.2f", occ_Max_f);
  421.     else
  422.     sprintf(hint_s[9], "   at pixel value %d", occ_Max_i);
  423.  
  424.     if (pix_format == PFFLOAT) {
  425.     sprintf(hint_s[10], "min pixel value = %.2f", val_min);
  426.     sprintf(hint_s[11], "Max pixel value = %.2f", val_Max);
  427.     } else {
  428.     sprintf(hint_s[10], "min pixel value = %d", (int) val_min);
  429.     sprintf(hint_s[11], "Max pixel value = %d", (int) val_Max);
  430.     }
  431.     hint_s[12][0] = 0;
  432.  
  433. #ifdef aay
  434.     switch (pix_format) {
  435.     case PFBYTE:
  436. /*     for (i=val_min; i<=val_Max; i++)
  437.        printf("b_hist[%d] = %d\n", i, b_hist[i]);  */
  438.     break;
  439.     case PFSHORT:
  440. /*     for (i=val_min+32768; i<=val_Max+32768; i++)
  441.        printf("s_hist[%d] = %d\n", i, s_hist[i]);  */
  442.     break;
  443.     case PFINT:
  444.     for (i = 0; i < n_of_dv; i++)
  445.         printf("i_hist[%d].pv=%d  i_hist[%d].occ=%d\n",
  446.            i, i_hist[i].pv, i, i_hist[i].occ);
  447.     break;
  448.     case PFFLOAT:
  449.     for (i = 0; i < n_of_dv; i++)
  450.         printf("f_hist[%d].pv=%.2f  f_hist[%d].occ=%d\n",
  451.            i, f_hist[i].pv, i, f_hist[i].occ);
  452.     break;
  453.     }
  454. #endif
  455.  
  456. }                /* end of set_str ()         */
  457.  
  458. comp_hg()
  459. {                /* to compute histogram graph     */
  460.     int       i;
  461.  
  462. #ifdef aax
  463.     printf("enter comp_hg, paint=%d  NewLeft=%d  NewRight=%d  NewTop=%d  NewBottom=%d",
  464.        paint, NewLeft, NewRight, NewTop, NewBottom);
  465. #endif
  466.  
  467.     switch (pix_format) {
  468.     case PFBYTE:
  469.     comp_byte_hg();        /* compute byte histogram graph         */
  470.     break;
  471.     case PFSHORT:
  472.     comp_short_hg();    /* compute byte histogram graph         */
  473.     break;
  474.     case PFINT:
  475.     comp_int_hg();        /* compute int histogram graph         */
  476.     break;
  477.     case PFFLOAT:
  478.     comp_float_hg();    /* compute byte histogram graph         */
  479.     break;
  480.     }
  481.  
  482.     cur_size = 0;        /* current size        */
  483.     for (i = 0; i <= range; i++)
  484.     cur_size = cur_size + histo_graph[i];
  485.  
  486.     if (range < 200)
  487.     expand();
  488.  
  489.     if (pix_format != PFFLOAT) {/* byte, short, int     */
  490. #ifdef aax
  491.     printf("in comp_hg, histo_va[0]=%d  histo_va[%d]=%d\n",
  492.            histo_va[0], range, histo_va[range]);
  493. #endif
  494.  
  495.     build_xlab(histo_va[0], histo_va[range]);
  496.  
  497. #ifdef aax
  498.     for (i = 0; i < end_x; i++)
  499.         printf("    x_label[%d]=%d\n", i, x_label[i]);
  500. #endif
  501.  
  502.     pv_diff = histo_va[range] - histo_va[0];
  503.     } else {
  504.     build_fxlab(fhisto_va[0], fhisto_va[range]);
  505.  
  506. #ifdef aax
  507.     for (i = 0; i < end_x; i++)
  508.         printf("    fx_label[%d]=%.2f\n", i, fx_label[i]);
  509. #endif
  510.  
  511.     pv_diff = fhisto_va[range] - fhisto_va[0];
  512.     }
  513.  
  514. #ifdef aax
  515.     printf("end of comp_hg,  Top=%d  Bottom=%d\n", Top, Bottom);
  516. #endif
  517.  
  518.     occ_diff = Top - Bottom;
  519.  
  520.     if (occ_diff > 30)
  521.     build_label(Bottom, Top);
  522.     else if (occ_diff > 1) {
  523.     y_label[0] = Bottom;
  524.     y_label[1] = (Bottom + Top) / 2;
  525.     y_label[2] = Top;
  526.     end_y = 3;
  527.     } else {
  528.     y_label[0] = Bottom;
  529.     y_label[1] = Top;
  530.     end_y = 2;
  531.     }
  532.  
  533. #ifdef aax
  534.     for (i = 0; i < end_y; i++)
  535.     printf("    y_label[%d]=%d\n", i, y_label[i]);
  536. #endif
  537.  
  538.     set_xy();
  539.  
  540. }                /* end of comp_hg ()         */
  541.  
  542. expand()
  543. {                /* as range < 200 after comp_..._hg         */
  544.     int       i, k;
  545.     int       hgr[220];
  546.     int       hv[220];
  547.     float     fhv[220];
  548.  
  549.     for (k = 0; k <= range; k++) {
  550.     hgr[k] = histo_graph[k];
  551.     if (pix_format != PFFLOAT)
  552.         hv[k] = histo_va[k];
  553.     else
  554.         fhv[k] = fhisto_va[k];
  555.     }
  556.  
  557.     histo_graph[201] = histo_graph[range + 1];
  558.  
  559.     if (pix_format != PFFLOAT)
  560.     histo_va[201] = histo_va[range + 1];
  561.     else
  562.     fhisto_va[201] = fhisto_va[range + 1];
  563.  
  564.     k = 0;            /* index of hgr[], hv[], fhv[]         */
  565.  
  566.     for (i = 0; i <= 200; i++) {
  567.     if (k <= range && k * 200 / (range + 1) == i) {
  568.         histo_graph[i] = hgr[k];
  569.         if (pix_format != PFFLOAT)
  570.         histo_va[i] = hv[k];
  571.         else
  572.         fhisto_va[i] = fhv[k];
  573.  
  574.         k++;
  575.     } else {
  576.         histo_graph[i] = histo_graph[i - 1];
  577.         if (pix_format != PFFLOAT)
  578.         histo_va[i] = histo_va[i - 1];
  579.         else
  580.         fhisto_va[i] = fhisto_va[i - 1];
  581.     }
  582.     }
  583.  
  584.     range = 200;
  585. }                /* end of  expand ()         */
  586.  
  587. set_xy()
  588. {                /* set x_pos[] and y_pos[]     */
  589.     int       i;
  590.  
  591.     for (i = 0; i < end_x; i++) {
  592.     if (pix_format != PFFLOAT)
  593.         x_pos[i] = 2 * TXTWD + range * (x_label[i] - histo_va[0]) / pv_diff;
  594.     else
  595.         x_pos[i] = 2 * TXTWD + range * (fx_label[i] - fhisto_va[0]) / pv_diff;
  596.     }
  597.  
  598.     for (i = 0; i < end_y; i++)
  599.     y_pos[i] = 3 * TXTHT + HGT - HGT * (y_label[i] - Bottom) / occ_diff;
  600.  
  601. }                /* end of set_xy ()         */
  602.  
  603. comp_byte_hg()
  604. {                /* compute byte histogram graph          */
  605.     int       i, k;
  606.     int       Left, Right;    /* index of b_hist[]       */
  607.  
  608.     if (paint == 0) {        /* fisrt picture of this image  */
  609.     Left = val_min;        /* index of b_hist[]     */
  610.     Right = val_Max;    /* index of b_hist[]       */
  611.     Top = occ_Max;
  612.     Bottom = 0;
  613.     paint = 1;
  614.     } else {
  615.     if (NewLeft != -1)
  616.         Left = histo_va[NewLeft];
  617.     else
  618.         Left = histo_va[0];    /* index of b_hist[]       */
  619.  
  620.     if (NewRight != -1)
  621.         Right = histo_va[NewRight];
  622.     else
  623.         Right = histo_va[range];    /* index of b_hist[]       */
  624.  
  625.     if (NewTop != -1)
  626.         Top = NewTop;
  627.     if (NewBottom != -1)
  628.         Bottom = NewBottom;
  629.     }
  630.  
  631. #ifdef aax
  632.     printf("in comp_byte_hg, Left=%d  Right=%d  Top=%d  Bottom=%d\n",
  633.        Left, Right, Top, Bottom);
  634. #endif
  635.  
  636.     range = Right - Left;    /* range of indices of histo_graph to show   */
  637.  
  638.     cur_nofdv = 0;        /* current # of different pixel values   */
  639.     for (i = 0; i <= range; i++) {
  640.     k = Left + i;
  641.  
  642.     if (b_hist[k] < Bottom)
  643.         histo_graph[i] = 0;
  644.     else {
  645.         if (b_hist[k] > 0)
  646.         cur_nofdv++;
  647.  
  648.         if (Top < b_hist[k])
  649.         histo_graph[i] = Top;
  650.         else
  651.         histo_graph[i] = b_hist[k];
  652.     }
  653.  
  654.     histo_va[i] = k;
  655.     }
  656.  
  657.     histo_va[range + 1] = Right + 1;
  658.  
  659. }                /* end of comp_byte_hg ()      */
  660.  
  661.  
  662. sif_occ()
  663. {                /* short, int, float occurrence change     */
  664.     int       i;
  665.  
  666.     if (NewTop == -1)
  667.     NewTop = Top;
  668.  
  669.     if (NewBottom == -1)
  670.     NewBottom = Bottom;
  671.  
  672.     Top = NewTop;
  673.     Bottom = NewBottom;
  674.  
  675.     for (i = 0; i <= range; i++) {
  676.     if (histo_graph[i] < Bottom)
  677.         histo_graph[i] = 0;
  678.     else if (Top < histo_graph[i])
  679.         histo_graph[i] = Top;
  680.     }
  681.  
  682. }
  683.  
  684. comp_short_hg()
  685. {                /* compute byte histogram graph          */
  686.     int       i, k;
  687.     int       Left, Right;    /* index of s_hist[]       */
  688.     float     m;
  689.  
  690.     if (paint == 0) {        /* fisrt picture of this image  */
  691.     Left = val_min + 32768;    /* index of s_hist[]     */
  692.     Right = val_Max + 32768;/* index of s_hist[]       */
  693.     paint = 1;
  694.     } else {
  695.     if (NewTop != -1 || NewBottom != -1) {
  696.         sif_occ();
  697.         return;
  698.     }
  699.     if (NewLeft != -1)
  700.         Left = histo_va[NewLeft] + 32768;
  701.     else
  702.         Left = histo_va[0] + 32768;    /* index of s_hist[]       */
  703.  
  704.     if (NewRight != -1)
  705.         Right = histo_va[NewRight + 1] - 1 + 32768;
  706.     else
  707.         Right = histo_va[range + 1] - 1 + 32768;    /* index of s_hist[]   */
  708.     }
  709.  
  710.     range = Right - Left;    /* range of indices of histo_graph to show   */
  711.  
  712.     if (range > 800) {
  713.     m = (range + 1) / 801.;
  714.     range = 800;
  715.     } else
  716.     m = 1;
  717.  
  718.     histo_va[range + 1] = Right + 1 - 32768;
  719.     for (i = 0; i <= range; i++)
  720.     histo_va[i] = Left + i * m - 32768;
  721.  
  722.     for (i = 0; i <= range; i++)
  723.     histo_graph[i] = 0;
  724.     i = 0;
  725.     cur_nofdv = 0;        /* current # of different pixel values   */
  726.     for (k = Left; k <= Right; k++) {
  727.     if (s_hist[k] > 0)
  728.         cur_nofdv++;
  729.     if (k - 32768 >= histo_va[i + 1])
  730.         i++;
  731.     histo_graph[i] = histo_graph[i] + s_hist[k];
  732.     }
  733.  
  734. #ifdef aax
  735.     printf("in comp_short_hg, range=%d  i=%d\n", range, i);
  736. #endif
  737.  
  738.     Top = histo_graph[0];
  739.     Bottom = histo_graph[0];
  740.     for (i = 1; i <= range; i++) {
  741.     if (Top < histo_graph[i])
  742.         Top = histo_graph[i];
  743.     if (Bottom > histo_graph[i])
  744.         Bottom = histo_graph[i];
  745.     }
  746.  
  747. #ifdef aax
  748.     printf("end of comp_short_hg, Left=%d  Right=%d  Top=%d  Bottom=%d\n",
  749.        Left, Right, Top, Bottom);
  750. #endif
  751.  
  752. }                /* end of  comp_short_hg ()         */
  753.  
  754.  
  755. comp_float_hg()
  756. {                /* compute byte histogram graph          */
  757.     int       i, k;
  758.     int       Left, Right;    /* index of f_hist[]       */
  759.     float     m;
  760.  
  761.     if (paint == 0) {        /* fisrt picture of this image  */
  762.     Left = 0;        /* index of f_hist[]     */
  763.     Right = n_of_dv - 1;    /* index of f_hist[]       */
  764.     paint = 1;
  765.     } else {
  766.     if (NewTop != -1 || NewBottom != -1) {
  767.         sif_occ();
  768.         return;
  769.     }
  770.     if (NewLeft != -1)
  771.         Left = fv_idx(Base, NewLeft);    /* index of f_hist[] */
  772.     else
  773.         Left = Base;    /* index of f_hist[]       */
  774.  
  775. /*
  776.    printf("in comp_float_hg, Left=%d  f_hist[%d].pv=%.2f  f_hist[%d].occ=%d\n",
  777.       Left, Left, f_hist[Left].pv, Left, f_hist[Left].occ );
  778. */
  779.  
  780.     if (NewRight != -1)
  781.         Right = fv_idx(Left, NewRight + 1);
  782.     else
  783.         Right = fv_idx(Left, range + 1);    /* index of f_hist[]   */
  784.     }
  785.  
  786.     cur_nofdv = Right - Left + 1;    /* current # of different pv     */
  787.  
  788.     Base = Left;        /* base index of f_hist[]   */
  789.  
  790.     range = (f_hist[Right].pv - f_hist[Left].pv) * 100;
  791.  
  792.     if (range > 800)
  793.     range = 800;
  794.  
  795.     m = (f_hist[Right].pv - f_hist[Left].pv) / range;
  796.  
  797.     fhisto_va[range + 1] = f_hist[Right].pv + .01;
  798.     for (i = 0; i <= range; i++)
  799.     fhisto_va[i] = f_hist[Left].pv + i * m;
  800.  
  801.     for (i = 0; i <= range; i++)
  802.     histo_graph[i] = 0;
  803.     i = 0;
  804.     for (k = Left; k <= Right; k++) {
  805.     while (f_hist[k].pv >= fhisto_va[i + 1])
  806.         i++;
  807.     histo_graph[i] = histo_graph[i] + f_hist[k].occ;
  808.     }
  809.  
  810. #ifdef aax
  811.     printf("in comp_float_hg, Base=%d  range=%d  i=%d\n", Base, range, i);
  812. #endif
  813.  
  814.     Top = histo_graph[0];
  815.     Bottom = histo_graph[0];
  816.     for (i = 1; i <= range; i++) {
  817.     if (Top < histo_graph[i])
  818.         Top = histo_graph[i];
  819.     if (Bottom > histo_graph[i])
  820.         Bottom = histo_graph[i];
  821.     }
  822.  
  823. #ifdef aax
  824.     printf("in comp_float_hg, Left=%d  Right=%d  Top=%d  Bottom=%d\n",
  825.        Left, Right, Top, Bottom);
  826. #endif
  827.  
  828. }                /* end of  comp_float_hg ()         */
  829.  
  830. int 
  831. fv_idx(Begin, Target)        /* float value --> index of f_list[].pv */
  832.     int       Begin;        /* the beginning index     */
  833.     int       Target;        /* target index of fhisto_va[]     */
  834. {
  835.     float     pv;
  836.     int       k;        /* index of f_list[].pv */
  837.  
  838.     pv = fhisto_va[Target];
  839.  
  840.     k = Begin;
  841.     do {
  842.     if (f_hist[k].pv >= pv)
  843.         return k;
  844.     k++;
  845.     } while (k < n_of_dv);
  846.  
  847.     return n_of_dv - 1;
  848. }
  849.  
  850.  
  851. comp_int_hg()
  852. {                /* compute byte histogram graph          */
  853.     int       i, k;
  854.     int       Left, Right;    /* index of i_hist[]       */
  855.     float     m;
  856.  
  857.     if (paint == 0) {        /* fisrt picture of this image  */
  858.     Left = 0;        /* index of i_hist[]     */
  859.     Right = n_of_dv - 1;    /* index of i_hist[]       */
  860.     paint = 1;
  861.     } else {
  862.     if (NewTop != -1 || NewBottom != -1) {
  863.         sif_occ();
  864.         return;
  865.     }
  866.     if (NewLeft != -1)
  867.         Left = iv_idx(Base, NewLeft);    /* index of i_hist[] */
  868.     else
  869.         Left = Base;    /* index of i_hist[]       */
  870.  
  871.     if (NewRight != -1)
  872.         Right = iv_idx(Left, NewRight + 1);
  873.     else
  874.         Right = iv_idx(Left, range + 1);    /* index of i_hist[]   */
  875.     }
  876.  
  877.     cur_nofdv = Right - Left + 1;    /* current # of different pv     */
  878.  
  879.     Base = Left;        /* base index of i_hist[]   */
  880.  
  881.     range = i_hist[Right].pv - i_hist[Left].pv;
  882.  
  883.     if (range > 800)
  884.     range = 800;
  885.  
  886.     m = (float) (i_hist[Right].pv - i_hist[Left].pv + 1) / (range + 1);
  887.  
  888.     histo_va[range + 1] = i_hist[Right].pv + 1;
  889.     for (i = 0; i <= range; i++)
  890.     histo_va[i] = i_hist[Left].pv + i * m;
  891.  
  892.     for (i = 0; i <= range; i++)
  893.     histo_graph[i] = 0;
  894.     i = 0;
  895.     for (k = Left; k <= Right; k++) {
  896.     while (i_hist[k].pv >= histo_va[i + 1])
  897.         i++;
  898.     histo_graph[i] = histo_graph[i] + i_hist[k].occ;
  899.     }
  900.  
  901. #ifdef aax
  902.     printf("in comp_int_hg, Base=%d  range=%d  i=%d\n", Base, range, i);
  903. #endif
  904.  
  905.     Top = histo_graph[0];
  906.     Bottom = histo_graph[0];
  907.     for (i = 1; i <= range; i++) {
  908.     if (Top < histo_graph[i])
  909.         Top = histo_graph[i];
  910.     if (Bottom > histo_graph[i])
  911.         Bottom = histo_graph[i];
  912.     }
  913.  
  914. #ifdef aax
  915.     printf("end of comp_int_hg, Left=%d  Right=%d  Top=%d  Bottom=%d\n",
  916.        Left, Right, Top, Bottom);
  917. #endif
  918.  
  919. }                /* end of  comp_int_hg ()         */
  920.  
  921. int 
  922. iv_idx(Begin, Target)        /* int value --> index of f_list[].pv */
  923.     int       Begin;        /* the beginning index     */
  924.     int       Target;        /* target index of histo_va[]     */
  925. {
  926.     int       pv;
  927.     int       k;        /* index of f_list[].pv */
  928.  
  929.     pv = histo_va[Target];
  930.  
  931.     k = Begin;
  932.     do {
  933.     if (i_hist[k].pv >= pv)
  934.         return k;
  935.     k++;
  936.     } while (k < n_of_dv);
  937.  
  938.     return n_of_dv - 1;
  939. }
  940.